<?php

class Australian_System_Auth {

    protected $_massegaError = null;
    protected $_page = null;

    public function login($arrParam, $options = null) {
        // 1. Goi ket noi voi Zend Db
        $db = Zend_Registry::get('connectDb');

        // 2. Khoi tao Zend Auth
        $auth = Zend_Auth::getInstance();
        $authAdapter = new Zend_Auth_Adapter_DbTable($db);

        $authAdapter->setTableName('user')
                ->setIdentityColumn('username')
                ->setCredentialColumn('password');
        $select = $authAdapter->getDbSelect();
        $select->where('status = 1');

        $encode = new Australian_Encode();
        $user_name = $arrParam['username'];
        $password = $encode->password($arrParam['password']);

        $authAdapter->setIdentity($user_name);
        $authAdapter->setCredential($password);

        // Lay ket qua truy van trong database
        $result = $auth->authenticate($authAdapter);

        $flag = false;
        if (!$result->isValid()) {
            $error = $result->getMessages();
            $this->_massegaError = current($error);
        } else {
            // Lay thong tin member dua vao session;
            $returnColumns      = array('userid', 'username', 'email', 'fullname', 'group_id');
            $data               = $authAdapter->getResultRowObject($returnColumns);
            $auth->getStorage()->write($data);
            $info               = $auth->getIdentity();
            $arrInfor           = (array)$info;
            $flag           = true;
            $gid            = $arrInfor['group_id'];
            $this->getController($gid);
            $session            = new Zend_Session_Namespace('userinfor');
            $session->groupid   = $gid;
            $session->username  = $arrInfor['username'];
            $session->userid    = $arrInfor['userid'];
            $session->setExpirationSeconds(3600);
        }

        return $flag;
    }

    public function getError() {
        return $this->_massegaError;
    }

    public function logout($arrParam = null, $options = null) {
        $auth = Zend_Auth::getInstance();
        $auth->clearIdentity();
        $session            = new Zend_Session_Namespace('userinfor');
        $session->unsetAll();
        $redirectHelper = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
        // call methods on the helper
        $redirectHelper->gotoUrl('/admin/login');
    }

    public function getController($gid) {
        $db = Zend_Registry::get('connectDb');
        $select     = $db->select()
                         ->from('permission AS p')
                         ->joinLeft('page AS pa', 'pa.id = p.pageid', 'controller')
                         ->where(" p.groupid = $gid AND p.view_p = 1 ");
        $rs = $db->fetchAll($select);
        if (count($rs) > 0) {
            $this->_page = $rs[0]['controller'];
        } else {
            $this->_page = 'login';
        }
    }
    
    public function getPage(){
        return $this->_page;
    }

}